package com.lzw.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.Announcement;
import com.lzw.service.admin.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/announcement")
@CrossOrigin(origins = "http://localhost:7070")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    //查询公告
    @GetMapping("/list")
    public PageInfo<Announcement> listAnnouncement(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate datePosted,
            String postedBy){
        PageHelper.startPage(pageNum,pageSize);
        List<Announcement> list = announcementService.listAnnouncement(pageNum, pageSize, datePosted, postedBy);
        return new PageInfo<>(list);
    }

    //添加公告
    @Log
    @PostMapping("/add")
    public String addAnnouncement(@RequestBody Announcement announcement){
        announcementService.addAnnouncement(announcement);
        return "添加成功";
    }

    //批量删除公告
    @Log
    @DeleteMapping("/delete/{ids}")
    public String deleteAnnouncement(@PathVariable List<Integer> ids){
       announcementService.deleteAnnouncement(ids);
       return "删除成功";
    }

    //更新公告
    @Log
    @PutMapping("/update")
    public String updateAnnouncement(@RequestBody Announcement announcement){
        announcementService.updateAnnouncement(announcement);
        return "更新成功";
    }

    //根据id查公告
    @GetMapping("/get/{id}")
    public Announcement getAnnouncementById(@PathVariable("id") int id){
        return announcementService.getAnnouncementById(id);
    }
}
